#ifndef _THREAD_POOL_H
#define _THREAD_POOL_H

#define USABLE_THREADS 1000

class DLL_EXPORT CControlInterface
{
public:
	ui32 m_threadid;
	HANDLE hThread;
public:
	ui32 GetId() { return m_threadid; }
	void FreezeThread();
	void RestartThread();
	void RegisterThread(HANDLE h) { hThread = h; }
};

struct Thread
{
	CControlInterface control;
	CThread* Execute;
	bool Kill;
};

class DLL_EXPORT CThreadPool
{
	friend class SanityChecker;
	set<Thread*> m_activethreads;
	set<Thread*> m_freethreads;
	set<Thread*> m_threadstoexit;
	Thread* StartThread(CThread*);
	Mutex m_freelock;
	Mutex m_activelock;
public:
	CThreadPool();
	void Start();
	void ShutDown();
	void ExecuteTask(CThread*);
	void RemoveFreeThread(Thread*);
	void AddActiveThread(Thread*);
	void RemoveActiveThread(Thread*);
	void SanityCheck();
	bool IsExiting(Thread* t)
	{
		return (m_threadstoexit.find(t) != m_threadstoexit.end());
	}
};

#endif